//index.js
//获取应用实例
var httpUtil = require("../../utils/http.js");
var datejs = require("dayjs")
var app = getApp()
Page({
  data: {
    show: false,
    lastTapTime: 0, // 用于记录上一次点击的时间戳
    a1src:'../../image/a1.png',
    a2src: '../../image/a2.png',
    a3src: '../../image/a3.png',
    a4src: '../../image/a4.png',
    signupimg:'../../image/signup.png',
    imgsrc:'http://hyvti.com/20220809/2b8444ed0ec548e699851fdaf025cbe2.jpg',
    iconsrc:'../../image/usercount.png',
    jtsrc:'../../image/icon-jt.png',
    imgUrls: [
      'https://www.yungee.cn/prod-api/profile/file/2025/10/17/e2010b4bc34d46539378538f32364c74.jpg',
      'https://www.yungee.cn/prod-api/profile/file/2025/10/17/0b7a7a81d75346149cc1d48f6ac76547.jpeg',
      'https://www.yungee.cn/prod-api/profile/file/2025/10/17/5c758adfb0f34253bcee97c92dfec732.jpeg',
      'https://www.yungee.cn/prod-api/profile/file/2025/10/17/c43db14837d54f0b86dd41bd3d3967e5.jpg',
      'https://www.yungee.cn/prod-api/profile/file/2025/10/17/81d7b5ffd315430b8b5d24ea3c2a7fb2.jpg'
    ],
    banners: [
    ],
    courses: [
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000 ,
    value: '',
    swiperHeight: 0,
    screenWidth: 0,
    imgRatio: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取屏幕宽度（用于计算图片高度）
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          screenWidth: res.windowWidth // 屏幕宽度（单位：px）
        });
      },
      fail: (err) => {
        console.error('获取系统信息失败', err);
      }
    });
    this.loadBanners();
    this.loadCourses();
  },
  onTabItemTap(item) {
     // 获取当前时间戳
     const currentTime = new Date().getTime();
     if (currentTime - this.data.lastTapTime < 200) {
        // 双击后的逻辑处理
        this.loadBanners();
        this.loadCourses();
     }
     this.setData({
        lastTapTime: currentTime
     })
    // 处理底部 tab 被点击的逻辑
  },

  onImageLoad: function(e) {
    const { width, height } = e.detail; // 图片原始宽高（px）
    const index = e.currentTarget.dataset.index; // 当前图片索引
    const ratio = height / width; // 计算宽高比（高 = 宽 * ratio）

    // 存储当前图片的宽高比
    const imgRatio = this.data.imgRatio;
    imgRatio[index] = ratio;
    this.setData({ imgRatio });

    // 计算swiper高度（屏幕宽度 * 宽高比）
    const swiperHeight = this.data.screenWidth * ratio;
    this.setData({ swiperHeight });
  },
  
  loadBanners: function(){
    const params = {
      url:  '/api/open/interface/getResult' ,
      method: "POST",
      data: {
        interfaceCode: "SI0001"
      },
      callBack: (res) => {
        console.log("res",res)
        this.setData({
          banners: res.data
        });
      },
      errCallBack:(err)=>{
        console.log("errCallBack",err)
      },
    };
    this.setData({
      banners: []
    });
    httpUtil.request(params,false);
  },
  loadCourses: function(){
    const params = {
      url:  '/api/open/interface/getResult' ,
      method: "POST",
      data: {
        interfaceCode: "SI0007",
        page: 1,
        size: 10
      },
      callBack: (res) => {
        console.log("res",res)
        this.setData({
          courses: res.data.rows
        });
      }
    };
    this.setData({
      courses: []
    });
    httpUtil.request(params,false);
  },


  onPullDownRefresh: function () {
    this.loadBanners();
    this.loadCourses();
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '教育模板首页',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
        console.log("转发成功")
      },
      fail: function (res) {
        // 转发失败
        onsole.log("转发失败")
      }
    }
  },

  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  onConfirm(event) {
    const [start, end] = event.detail;
    this.setData({
      show: false,
      date: `${datejs(start).format('YYYY/MM/DD')} - ${datejs(end).format('YYYY/MM/DD')}`,
    });
  },
  onSearch: function(e){
    console.log(e.detail)
  }
})
